package menus;
import gui.DefenderGUI;
import gui.PosJugadoresGui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.LinkedList;

import data.Database;

import Aplicacion.MP3Player;
import Aplicacion.Variables_globales;
import Entidades.Jugadores;
import Entidades.Paises;
import Entidades.Posesion;

public class Defender implements CustomButton {
	private final Database db = new Database();
	
	private boolean btnPress = false;
	private boolean btnHover = false;
	private boolean btnClick = false;
	
	private LinkedList<Paises> paises;
	private LinkedList<Posesion> posesion;
	
	private Paises selected_pais = null;
	
	private int x = 0;
	private int y = 0;
	private int popupX = x;
	private int popupY = y;
	
	public Defender () {
		paises = db.getPaises();
	}

	@Override
	public Graphics paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		this.isClicked();
		
		posesion = db.getPosesion();
		
		// paises
		for (Paises p: paises) {
			g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
			g2d.setColor(new Color(138, 138, 138));
			for (Posesion po: posesion)	{
				for (Jugadores j: Variables_globales.LISTA_JUGADORES) {
					if (po.getIDJugador() == j.getIDJugador() && po.getIDPais() == p.getIdPais()) {
						g2d.setColor(j.getColor());
						break;
					}
				}
			}
			g2d.fillOval((p.getX()+(p.getWidth()/2)), (p.getY()+(p.getHeight()/2)), 30, 30);
			g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
			g2d.setColor(Color.WHITE);
			
			boolean tmp = false;
			for (Posesion po: posesion)	{
				if (po.getIDPais() == p.getIdPais()) {
					tmp = true;
					g2d.drawString(po.getSoldados()+"", (p.getX()+(p.getWidth()/2)+11), (p.getY()+(p.getHeight()/2)+20));
				}
			}
			
			if (!tmp) g2d.drawString("0", (p.getX()+(p.getWidth()/2)+11), (p.getY()+(p.getHeight()/2)+20));
		}
		
		if (selected_pais != null) g = this.paintMenu(g);
		return g;
	}
	
	public synchronized Graphics paintMenu (Graphics g) {
		if (selected_pais != null) {
			if (db.estePaisTieneDuenoOTePertenece(selected_pais.getIdPais())) {
				new DefenderGUI(selected_pais.getIdPais(), selected_pais.getNombre(), popupX, popupY);
				System.err.println(Variables_globales.PAUSA);
				Variables_globales.PAUSA = true;
			}
		}
		selected_pais = null;
		return g;
	}
	
	public void isClicked () {
		for (Paises p: paises) {
			if (x > (p.getX()+(p.getWidth()/2)) && x < ((p.getX()+(p.getWidth()/2))+30)) {
				if (y > (p.getY()+(p.getHeight()/2)) && y < ((p.getY()+(p.getHeight()/2))+30)) {
					selected_pais = p;
					
					try {
						MP3Player WAV = new MP3Player();
						WAV.playDaemon(this.getClass().getResource("/Imagenes/clickCountry.mp3").toString());
					} catch (IllegalThreadStateException ex) {}
				}
			}
		}
		
		this.popupX = x;
		this.popupY = y;
		
		this.x = 0;
		this.y = 0;
	}

	@Override
	public void setBtnPress(boolean btnPress) {
		this.btnPress = btnPress;
	}
	
	@Override
	public void setBtnHover(boolean btnHover) {
		this.btnHover = btnHover;
	}

	@Override
	public void setBtnClick(boolean btnClick) {
		this.btnClick = btnClick;
	}

	@Override
	public void update(int x, int y) {
		this.x = x;
		this.y = y;
	}
}
